﻿using System.Collections.Generic;
using System.Linq;
using Missbot.Common;
using Missbot.Model.MsgInfos;

namespace Missbot.AIModule.Pet.PetAgainst
{
    public class PetAgainstSvc : ISingleton
    {
        private List<PetAgainstEngine> Engines { get; }

        public PetLevelSvc PetLevelSvc { get; set; }

        public PetAgainstSvc()
        {
            Engines = new List<PetAgainstEngine>();
        }

        public bool CheckGroup(string GroupID)
        {
            return Engines.All(p => p.Group.ID != GroupID);
        }

        public bool CheckUser(string UserID)
        {
            return Engines.All(p => p.SelfPet.User.ID != UserID && p.AimPet.User.ID != UserID);
        }

        public void StartGame(InstanceUser selfUser, InstanceUser aimUser, InstanceGroup Group)
        {
            var selfPet = PetRecordDA.Get(selfUser.ID);
            var aimPet = PetRecordDA.Get(aimUser.ID);
            var engine = new PetAgainstEngine
            {
                SelfPet = new GamingPet
                {
                    Name = selfPet.Name,
                    HP = PetLevelSvc[selfPet.Level].HP,
                    User = selfUser,
                    Skills = new Dictionary<string, int>(selfPet.Skills),
                    Level = selfPet.Level
                },
                AimPet = new GamingPet
                {
                    Name = aimPet.Name,
                    HP = PetLevelSvc[aimPet.Level].HP,
                    User = aimUser,
                    Skills = new Dictionary<string, int>(aimPet.Skills),
                    Level = aimPet.Level
                },
                Group = Group
            };
            Engines.Add(engine);
            engine.StartGame();
            Engines.Remove(engine);
        }
    }
}